package com.xnf.ssmweb.dao;

import com.xnf.ssmweb.domain.Permission;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * Created by chendx on 2021/3/30.
 */

public interface PermissionMapper {


    @Select("select * from permission where id in (select permissionId from role_permission where roleId = #{roleId})")
    @Results({
            @Result(id = true,column = "id" ,property = "id"),
            @Result(column = "permissionName" , property = "permissionName"),
            @Result(column = "url",property = "url"),
            @Result(column = "id",property = "roles",javaType = List.class,
                    many = @Many(select = "com.xnf.ssmweb.dao.RoleMapper.findRoleByPermissionId"))
    })
    List<Permission> findPermissionByRoleId(Long roleId);

    @Select("select * from permission")
    @Results({
            @Result(id = true,column = "id" ,property = "id"),
            @Result(column = "permissionName" , property = "permissionName"),
            @Result(column = "url",property = "url"),
            @Result(column = "id",property = "roles",javaType = List.class,
            many = @Many(select = "com.xnf.ssmweb.dao.RoleMapper.findRoleByPermissionId"))
    })
    List<Permission> findAll();

    @Select("select * from permission where id = #{id}")
    @Results({
            @Result(id = true,column = "id" ,property = "id"),
            @Result(column = "permissionName" , property = "permissionName"),
            @Result(column = "url",property = "url"),
            @Result(column = "id",property = "roles",javaType = List.class,
                    many = @Many(select = "com.xnf.ssmweb.dao.RoleMapper.findRoleByPermissionId"))
    })
    Permission findById(Long id);

    @Insert("insert into permission (permissionName,url) values (#{permissionName},#{url})")
    void save(Permission permission);
}
